console.log("", data);

//设置当前正在播放的歌曲的索引
var currentIndex = 0;
//声明一个标记，记录歌曲的播放状态
var isplay = false;
// audio_js.defaultMuted 静音
// audio_js.volume 声音 0-1


var begBACK_js = document.querySelector(".backGroundImg");
var begback_js = document.querySelector(".begbox");
var keyClick = true;
var keyLa = null;
//拖拽进度条
var misic_bars_js = document.querySelector('.misic_Bars');


//给进度条外层绑定点击滑动事件
var key = true;
window.addEventListener("load", function () {
  
  misic_bars_js.onmousedown = function () {
    // console.log('',event);
    var that = this
    var width_bars = this.offsetWidth;
    window.onmousemove = function (e) {
      var maginLeft = document.querySelector('.box')
      var left = e.pageX - that.offsetLeft  - maginLeft.offsetLeft;
      left = Math.min(Math.max(0, left), width_bars);
      // console.log('', left / width_bars * 100);
      // console.log('',audio_js.currentTime = left / width_bars * 100);
      // audioPlay(left / width_bars)
      //这里有错
      misic_line.style.width = left / width_bars * 100 + '%';
      audio_js.currentTime = audio_js.duration * left / width_bars
      // console.log('',left / width_bars);
      key = false
    }
  }
  window.onmouseup = function () {
    window.onmousemove = null
    // console.log('鼠标抬起');
    key = true
  }




  LayerJs.onclick = function () {
    if (keyClick) {
      this.childNodes[1].className = "iconfont icon-24gf-pause2";
      begBACK_js.style.animationPlayState = "running"; //开始旋转
      audio_js.play();
      // console.log('',this.childNodes[1].className);
      keyClick = false;
    } else {
      this.childNodes[1].className = "iconfont icon-24gl-play";
      begBACK_js.style.animationPlayState = "paused";
      audio_js.pause();
      keyClick = true;
    }
  };
  //上一首
  left_js.onclick = function () {
    if (currentIndex > 0) {
      currentIndex--;
      render(data[currentIndex]);
      audio_js.play();
    } else {
      currentIndex = data.length - 1;
      render(data[currentIndex]);
      audio_js.play();
      LayerJs.childNodes[1].className = "iconfont icon-24gf-pause2";
      begBACK_js.style.animationPlayState = "running"; //开始旋转
      // keyClick = true
    }
    keyClick = false;
  };
  //下一首
    right_js.onclick = function nextFun() {
    if (currentIndex < data.length - 1) {
      currentIndex++;
      render(data[currentIndex]);
      audio_js.play();
      LayerJs.childNodes[1].className = "iconfont icon-24gf-pause2";
      
      begBACK_js.style.animationPlayState = "running"; //开始旋转
      // keyClick = true
      // console.log('',currentIndex);
    } else {
      currentIndex = 0;
      render(data[currentIndex]);
      audio_js.play();
      LayerJs.childNodes[1].className = "iconfont icon-24gf-pause2";
      begBACK_js.style.animationPlayState = "running"; //开始旋转
      // keyClick = true
    }
    keyClick = false;
    };
    
  //监听audio标签的timeupdate 事件
  audio_js.ontimeupdate = function () {
    //获取当前播放到的时间 单位 s
      var currentTime, duration

      duration = audio_js.duration;
      currentTime = audio_js.currentTime;

    //  设置当前播放时间
    StartSpan.innerText = dataTime(currentTime);
    // console.log(dataTime(currentTime), dataTime(duration));
    //设置进度条

    if (key) {
      var value = (currentTime / duration) * 100 || 0;
      misic_line.style.width = value + "%";
    }
      // key = false

    dataText(data[currentIndex], currentTime);
    // console.log('',data.length,currentIndex);
      if (currentTime == duration) {
        currentIndex = currentIndex < data.length-1? ++currentIndex  : 0;
        render(data[currentIndex]);
        console.log('',currentIndex);
            audio_js.play();
            LayerJs.childNodes[1].className = "iconfont icon-24gf-pause2";
          begBACK_js.style.animationPlayState = "running"; //开始旋转
          // console.log('',begBACK_js.style.transform);
      }
  };
  
  //格式化歌词
  function dataText(text, dataTime) {
    var lrcTextList = text.lrclist;
    var timeFoor = Math.floor(dataTime * 100) / 100;
    for (let i = 0; i < lrcTextList.length; i++) {
      var textLin = lrcTextList[i].lineLyric;
      var time = lrcTextList[i].time;
    //   console.log("", time, dataTime);
      if (parseInt(timeFoor - time) == 0) {
        lyric.innerText = textLin;
        // console.log('成功',textLin);
      }
    }
  }


  //静音
  mute_js.onclick = function () {
    audio_js.muted = true
    this.style.color = 'red';
    play_js.style,color = ''
    console.log('',audio_js.muted);
  }
  play_js.onclick = function () {
    audio_js.muted = false;
    this.style.color = 'red';
    mute_js.style,color = ''
    console.log('',audio_js.muted );
  }
  //格式化时间
  function dataTime(time) {
    var min = parseInt(time / 60);
    var sec = parseInt(time % 60);
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;
    return `${min}:${sec}`;
  }
  //根据信息渲染页面
  function render(list) {
    nameText_js.innerText = list.songinfo.album;
    nameSmall_js.innerText = list.songinfo.artist;

    begBACK_js.style.background = `url(${list.songinfo.pic})`;
    begback_js.style.background =
      `url(${list.songinfo.pic})` + "no-repeat center center";
    begback_js.style.backgroundSize = "cover";
    endSpan.innerText = list.songinfo.songTimeMinutes;
    audio_js.src = list.url;

    // console.log('',audio_js.src);
  }
  render(data[currentIndex]);
});
